package com.example.za.account.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.za.account.R;
import com.example.za.account.dao.IncomeCatDao;
import com.example.za.account.dao.IncomeDao;
import com.example.za.account.model.IncomeCat;
import com.example.za.account.utils.T;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GridInCatAdapter extends BaseAdapter {

    private List<IncomeCat> incomeCats;
    private Context context;
    private SparseArray<ImageView> imageViewSparseArray;

    public GridInCatAdapter(Context context, List<IncomeCat> incomeCats) {
        this.context = context;
        this.incomeCats = incomeCats;
        this.imageViewSparseArray = new SparseArray<>();
    }

    @Override
    public int getCount() {
        return incomeCats != null ? incomeCats.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return incomeCats != null ? incomeCats.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_category, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.ivCategory = (ImageView) convertView.findViewById(R.id.item_category_icon);
            viewHolder.ivClose = (ImageView) convertView.findViewById(R.id.item_category_close);
            viewHolder.tvCategory = (TextView) convertView.findViewById(R.id.item_category_label);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final IncomeCat incomeCat = incomeCats.get(position);
        String name = incomeCat.getmName();
        if (!"添加".equals(name) && !"删除".equals(name)) {
            viewHolder.ivClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //先判断该类别是否有数据
                    IncomeCatDao incomeCatDao = new IncomeCatDao(context);
                    IncomeDao incomeDao = new IncomeDao(context);
                    Map<String, Object> map = new HashMap<>();
                    map.put("AScategoryId", incomeCat);
                    if (!incomeDao.existsInfo(map)) {
                        if (incomeCatDao.delete(incomeCat)) {
                            T.showShort(context, "删除成功");
                            incomeCats.remove(position);
                            notifyDataSetChanged();
                            setCloseVisibility(View.GONE);
                            imageViewSparseArray.remove(imageViewSparseArray.size() - 1);
                        } else {
                            T.showShort(context, "删除失败");
                            setCloseVisibility(View.GONE);
                        }
                    } else {
                        T.showShort(context, "该类别存在账目，不可删除。");
                    }
                }
            });
            imageViewSparseArray.put(position,viewHolder.ivClose);
        }
        viewHolder.tvCategory.setText(name);
        viewHolder.ivCategory.setImageResource(incomeCat.getmImageId());
        return convertView;
    }

    public void setCloseVisibility(int visible) {
        for (int i = 0, j = imageViewSparseArray.size(); i < j; i++) {
            if (visible == View.VISIBLE) {
                imageViewSparseArray.get(i).setVisibility(View.VISIBLE);
            } else {
                imageViewSparseArray.get(i).setVisibility(View.GONE);
            }
        }
    }

    public void setData(List<IncomeCat> incomeCats) {
        this.incomeCats = incomeCats;
        notifyDataSetChanged();
    }

    public int getCloseVisibility() {
        return imageViewSparseArray.get(0).getVisibility();
    }

    static class ViewHolder {
        ImageView ivClose;
        ImageView ivCategory;
        TextView tvCategory;
    }

}
